package org.projecteboot.reptile.common.task;

import lombok.extern.slf4j.Slf4j;
import org.projecteboot.reptile.common.entity.ReptilePage;
import org.projecteboot.reptile.common.pageprocessor.CommonPageProcessor;
import org.projecteboot.reptile.common.service.ReptilePageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Spider;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author LuoQiang
 * @date 2019/8/30 10:09
 **/
@Component
@Slf4j
public class ReptileTask {
    @Autowired
    private CommonPageProcessor commonPageProcessor;
    @Autowired
    private ReptilePageService reptilePageService;
    private Spider spider;

    @PostConstruct
    public void init() {
        log.info("线程启动");
        spider = Spider.create(commonPageProcessor);
        List<ReptilePage> reptilePages = reptilePageService.listBySuccess("NO");
        for (ReptilePage reptilePage : reptilePages) {
            spider.addUrl(reptilePage.getProtocol().toLowerCase() + "://" + reptilePage.getUrl());
        }
        spider.start();
    }


}
